package com.twang.stock.mq.rocketMq;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.dto.EmailMessage;
import com.twang.stock.email.SendmailService;
import com.twang.stock.threadPool.AsycTaskExecutorConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Component
@Slf4j
public class EmailWranConsumer {
    @Autowired
    private SendmailService sendmailService;
    @Autowired
    private AsycTaskExecutorConfig asycTaskExecutorConfig;

    public void receiveMessage(String message) throws UnknownHostException {
       log.info("==========> 接收到消息:email.warn.stock.routing.key IP={} message={}  ", InetAddress.getLocalHost().getHostAddress(), message);
            try{
                if(StringUtils.isNotEmpty(message)) {
                    EmailMessage emailMessage = JSONObject.parseObject(message, EmailMessage.class);
                    asycTaskExecutorConfig.taskQueueExecutor().execute(() -> {

                        //做T
                        if (StringUtils.isEmpty(emailMessage.getType())) {
                            //String[] str = message.split("\\|");
                            sendmailService.sendMailWithByZuoT(emailMessage.getMessage(), emailMessage.getName(), emailMessage.getName(), emailMessage.getEmail());
                        }
                        //异动
                        if ("1".equals(emailMessage.getType())) {

                            sendmailService.sendMailWithEmail(emailMessage.getMessage(), emailMessage.getName(), emailMessage.getName(), emailMessage.getUserId());
                        }
                        //热点变动
                        if ("2".equals(emailMessage.getType())) {

                            sendmailService.sendMailWithEmail(emailMessage.getMessage(), emailMessage.getName(), emailMessage.getName(), emailMessage.getUserId());
                        }

                        //触发分时涨幅超过2%
                        if ("3".equals(emailMessage.getType())) {
                            sendmailService.触发分时涨幅超过(emailMessage.getMessage(), emailMessage.getName(), emailMessage.getName(), emailMessage.getEmail());
                        }

                        //触发分时涨幅超过2%
                        if ("4".equals(emailMessage.getType())) {
                            sendmailService.触发成交量超过60天最高(emailMessage.getMessage(), emailMessage.getName(), emailMessage.getName(), emailMessage.getEmail());
                        }
                    });
                }
                log.info("==========> 分时处理结束 IP:{}  message:{}", InetAddress.getLocalHost().getHostAddress(),message);
            }catch (Exception e){
                log.error("{}",e);
            }
    }
}

